package com.zygh.lz.stapprove;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zygh.lz.constant.SystemCon;
import com.zygh.lz.vo.FieldValues;
import com.zygh.lz.vo.PageBean;
import com.zygh.lz.vo.ResultBean;
import com.zygh.lz.vo.ResultBeans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ResultUtil {

    //操作成功
    public static ResultBean setOK(String msg, Object data) {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(1);
        resultBean.setData(data);
        resultBean.setMsg(msg);
        return resultBean;
    }

    //操作失败
    public static ResultBean setError(int code, String msg, Object data) {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(code);
        resultBean.setData(data);
        resultBean.setMsg(msg);
        return resultBean;
    }

    //分页操作
    //操作成功
    public static PageBean setPageOK(int page, int limit, int count, List<Object> data) {
        PageBean pageBean = new PageBean();
        pageBean.setCode(1);
        pageBean.setData(data);
        pageBean.setMsg("OK");
        pageBean.setLimit(limit);
        return pageBean;
    }

    public static PageBean setPageError(List<Object> data) {
        PageBean pageBean = new PageBean();
        pageBean.setCode(0);
        pageBean.setData(null);
        pageBean.setMsg("error");

        return pageBean;
    }

    //非查询操作
    public static ResultBean execOp(int count, String msg) {
        if (count > 0) {
            return setOK(msg + "成功", null);
        } else {
            return setError(SystemCon.RERROR1, msg + "失败", null);
        }
    }

    //成功操作返回的resultbeans的格式
    public static ResultBeans resultBeansSuccess(Object obj) {
        ResultBeans resultBeans = new ResultBeans();
        resultBeans.setCode(200);
        resultBeans.setMessage("success");
        Map map = new HashMap();
        FieldValues fieldValues = new FieldValues();
        List<FieldValues> list = new ArrayList<>();
        fieldValues.setField("json");
        fieldValues.setValue(JSON.toJSONString(obj));
        list.add(fieldValues);
        map.put("fieldValues", list);
        resultBeans.getData().getDataList().add(map);
        return resultBeans;
    }

    public static ResultBeans resultBeansError(Object obj) {
        ResultBeans resultBeans = new ResultBeans();
        resultBeans.setCode(500);
        resultBeans.setMessage("error");
        Map map = new HashMap();
        List<FieldValues> list = new ArrayList<>();
        FieldValues fieldValues = new FieldValues();
        fieldValues.setField("json");
        fieldValues.setValue(JSON.toJSONString(obj));
        list.add(fieldValues);
        map.put("fieldValues", list);
        resultBeans.getData().getDataList().add(map);
        return resultBeans;
    }

    public static ResultBeans resultBeansError(String msg, Object obj) {
        ResultBeans resultBeans = new ResultBeans();
        resultBeans.setCode(500);
        resultBeans.setMessage(msg);
        Map map = new HashMap();
        List<FieldValues> list = new ArrayList<>();
        FieldValues fieldValues = new FieldValues();
        fieldValues.setField("json");
        fieldValues.setValue(JSON.toJSONString(obj));
        list.add(fieldValues);
        map.put("fieldValues", list);
        resultBeans.getData().getDataList().add(map);
        return resultBeans;
    }

    public static ResultBeans resultBeansCodeError(Integer code, String msg, Object obj) {
        ResultBeans resultBeans = new ResultBeans();
        resultBeans.setCode(code);
        resultBeans.setMessage(msg);
        Map map = new HashMap();
        List<FieldValues> list = new ArrayList<>();
        FieldValues fieldValues = new FieldValues();
        fieldValues.setField("json");
        fieldValues.setValue(JSON.toJSONString(obj));
        list.add(fieldValues);
        map.put("fieldValues", list);
        resultBeans.getData().getDataList().add(map);
        return resultBeans;
    }

    public static ResultBeans resultBeansexecOp(int count, String msg) {
        if (count > 0) {
            return resultBeansSuccess(msg + "成功");
        } else {
            return resultBeansError(msg + "失败");
        }
    }

    public static ResultBeans resultBeans(ResultBeans resultBean, String content) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(content);
            resultBean.setMessageId(jsonObject.getString("messageId"));
            System.out.println("---------------------------------->messageId" + jsonObject.getString("messageId") + "---------------------------------->");
            resultBean.setVersion(jsonObject.getString("version"));
            resultBean.getData().getPage().setPageSize(jsonObject.getJSONObject("parameter").getJSONObject("page").getInteger("pageSize"));
            resultBean.getData().getPage().setPageNo(jsonObject.getJSONObject("parameter").getJSONObject("page").getInteger("pageNo"));

        } catch (Exception e) {
            System.out.println("请求体为空");
        }
        return resultBean;
    }
}